<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="提供者" prop="provider">
        <el-input v-model="formData.provider" placeholder="请输入提供者" />
      </el-form-item>
      <el-form-item label="应用发布国家" prop="providerCountry">
        <el-input v-model="formData.providerCountry" placeholder="请输入应用发布国家" />
      </el-form-item>
      <el-form-item label="SKU" prop="sku">
        <el-input v-model="formData.sku" placeholder="请输入SKU" />
      </el-form-item>
      <el-form-item label="开发者" prop="developer">
        <el-input v-model="formData.developer" placeholder="请输入开发者" />
      </el-form-item>
      <el-form-item label="产品名称" prop="title">
        <el-input v-model="formData.title" placeholder="请输入产品名称" />
      </el-form-item>
      <el-form-item label="版本" prop="version">
        <el-input v-model="formData.version" placeholder="请输入版本" />
      </el-form-item>
      <el-form-item label="产品标识" prop="productTypeIdentifier">
        <el-input v-model="formData.productTypeIdentifier" placeholder="请输入产品标识" />
      </el-form-item>
      <el-form-item label="安装数" prop="units">
        <el-input v-model="formData.units" placeholder="请输入安装数" />
      </el-form-item>
      <el-form-item label="收入" prop="developerProceeds">
        <el-input v-model="formData.developerProceeds" placeholder="请输入收入" />
      </el-form-item>
      <el-form-item label="开始日期" prop="beginDate">
        <el-date-picker
          v-model="formData.beginDate"
          type="date"
          value-format="x"
          placeholder="选择开始日期"
        />
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker
          v-model="formData.endDate"
          type="date"
          value-format="x"
          placeholder="选择结束日期"
        />
      </el-form-item>
      <el-form-item label="货币符号" prop="customerCurrency">
        <el-input v-model="formData.customerCurrency" placeholder="请输入货币符号" />
      </el-form-item>
      <el-form-item label="国家" prop="countryCode">
        <el-input v-model="formData.countryCode" placeholder="请输入国家" />
      </el-form-item>
      <el-form-item label="货币符号2" prop="currencyOfProceeds">
        <el-input v-model="formData.currencyOfProceeds" placeholder="请输入货币符号2" />
      </el-form-item>
      <el-form-item label="产品 ID" prop="appleIdentifier">
        <el-input v-model="formData.appleIdentifier" placeholder="请输入产品 ID" />
      </el-form-item>
      <el-form-item label="单价" prop="customerPrice">
        <el-input v-model="formData.customerPrice" placeholder="请输入单价" />
      </el-form-item>
      <el-form-item label="推广码" prop="promoCode">
        <el-input v-model="formData.promoCode" placeholder="请输入推广码" />
      </el-form-item>
      <el-form-item label="父应用" prop="parentIdentifier">
        <el-input v-model="formData.parentIdentifier" placeholder="请输入父应用" />
      </el-form-item>
      <el-form-item label="订阅类型" prop="subscription">
        <el-input v-model="formData.subscription" placeholder="请输入订阅类型" />
      </el-form-item>
      <el-form-item label="订阅周期" prop="period">
        <el-input v-model="formData.period" placeholder="请输入订阅周期" />
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-input v-model="formData.category" placeholder="请输入分类" />
      </el-form-item>
      <el-form-item label="CMB" prop="cmb">
        <el-input v-model="formData.cmb" placeholder="请输入CMB" />
      </el-form-item>
      <el-form-item label="设备类型" prop="device">
        <el-input v-model="formData.device" placeholder="请输入设备类型" />
      </el-form-item>
      <el-form-item label="系统" prop="supportedPlatforms">
        <el-input v-model="formData.supportedPlatforms" placeholder="请输入系统" />
      </el-form-item>
      <el-form-item label="原因" prop="proceedsReason">
        <el-input v-model="formData.proceedsReason" placeholder="请输入原因" />
      </el-form-item>
      <el-form-item label="退款" prop="preservedPricing">
        <el-input v-model="formData.preservedPricing" placeholder="请输入退款" />
      </el-form-item>
      <el-form-item label="客户端" prop="client">
        <el-input v-model="formData.client" placeholder="请输入客户端" />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="formData.orderType" placeholder="请选择订单类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { SalesReportApi, SalesReportVO } from '@/api/ascsc/salesreport'

/** ASA管理系统 表单 */
defineOptions({ name: 'SalesReportForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  provider: undefined,
  providerCountry: undefined,
  sku: undefined,
  developer: undefined,
  title: undefined,
  version: undefined,
  productTypeIdentifier: undefined,
  units: undefined,
  developerProceeds: undefined,
  beginDate: undefined,
  endDate: undefined,
  customerCurrency: undefined,
  countryCode: undefined,
  currencyOfProceeds: undefined,
  appleIdentifier: undefined,
  customerPrice: undefined,
  promoCode: undefined,
  parentIdentifier: undefined,
  subscription: undefined,
  period: undefined,
  category: undefined,
  cmb: undefined,
  device: undefined,
  supportedPlatforms: undefined,
  proceedsReason: undefined,
  preservedPricing: undefined,
  client: undefined,
  orderType: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await SalesReportApi.getSalesReport(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SalesReportVO
    if (formType.value === 'create') {
      await SalesReportApi.createSalesReport(data)
      message.success(t('common.createSuccess'))
    } else {
      await SalesReportApi.updateSalesReport(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    provider: undefined,
    providerCountry: undefined,
    sku: undefined,
    developer: undefined,
    title: undefined,
    version: undefined,
    productTypeIdentifier: undefined,
    units: undefined,
    developerProceeds: undefined,
    beginDate: undefined,
    endDate: undefined,
    customerCurrency: undefined,
    countryCode: undefined,
    currencyOfProceeds: undefined,
    appleIdentifier: undefined,
    customerPrice: undefined,
    promoCode: undefined,
    parentIdentifier: undefined,
    subscription: undefined,
    period: undefined,
    category: undefined,
    cmb: undefined,
    device: undefined,
    supportedPlatforms: undefined,
    proceedsReason: undefined,
    preservedPricing: undefined,
    client: undefined,
    orderType: undefined
  }
  formRef.value?.resetFields()
}
</script>
